﻿using BackEnd.Common;
using BackEnd.Models;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BackEnd.Controllers
{
    public class MetaHeaderController : Controller
    {
        //
        // GET: /MetaHeader/
        [CustomAuthorize(Action = Constants.AdminActions.ViewMetaHeader)]
        public ActionResult Index()
        {
            var subBL = new MetaHeaderBL();
            var lstAdminActionGroups = new List<MetaHeader>();
            lstAdminActionGroups.AddRange(subBL.GetAll());

            return View("Index", lstAdminActionGroups);
        }
        //
        // GET:/MetaHeader/Create
        [CustomAuthorize(Action = Constants.AdminActions.CreateMetaHeader)]
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.CreateMetaHeader)]
        public ActionResult Create(MetaHeaderModel model)
        {
            if (ModelState.IsValid)
            {
                var subsBL = new MetaHeaderBL();
                var check = subsBL.GetByUrl(model.Url);
                if (check == null)
                {
                    var subs = new MetaHeader();
                    subs.Title = model.Title;
                    subs.Url = model.Url;
                    subs.KeyWord = model.KeyWord;
                    subs.Description = model.Description;

                    int result = subsBL.CreateMetaHeader(subs);
                    if (result > 0)
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ViewData["ErrorMessage"] = "Has an error when executing insert MetaHeader into database";
                    }
                }
                else
                {
                    ViewData["ErrorMessage"] = String.Format("This MetaHeader with Url - {0} exists!", model.Url);

                }

            }

            return View(model);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [CustomAuthorize(Action = Constants.AdminActions.DeleteMetaHeader)]
        public ActionResult Delete(string id)
        {

            var subsBL = new MetaHeaderBL();
            if (!String.IsNullOrEmpty(id))
            {
                List<string> lstOfSubsIDs = new List<string>();
                foreach (string item in id.Split('_'))
                {
                    int temp = -1;
                    Int32.TryParse(item, out temp);
                    if (temp > 0)
                    {
                        lstOfSubsIDs.Add(item);
                    }
                }
                int result = subsBL.DeleteMetaHeaderByIds(lstOfSubsIDs);
                if (result > 0)
                {
                    //return Json(1, JsonRequestBehavior.AllowGet);
                    ViewData["ErrorMessage"] = "Delete successful!";
                    return RedirectToAction("Index");
                }

            }
            //return Json(0, JsonRequestBehavior.AllowGet);
            ViewData["ErrorMessage"] = "Cannot delete selected items!";
            return RedirectToAction("Index");


        }
        //
        // GET:/MetaHeader/Edit/id
        //[AcceptVerbs(HttpVerbs.Get)]
        [CustomAuthorize(Action = Constants.AdminActions.UpdateMetaHeader)]
        public ActionResult Edit(int id)
        {
            var subBL = new MetaHeaderBL();
            var subs = subBL.GetByID(id);
            var subModel = new MetaHeaderModel();
            subModel.Id = subs.Id;
            subModel.Title = subs.Title;
            subModel.Url = subs.Url;
            subModel.KeyWord = subs.KeyWord;
            subModel.Description = subs.Description;

            return View("Edit", subModel);
        }
        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.UpdateMetaHeader)]
        public ActionResult Edit(MetaHeaderModel model)
        {
            if (ModelState.IsValid)
            {
                var subsBL = new MetaHeaderBL();
                var check = subsBL.GetByID(model.Id);
                if (check != null)
                {
                    if (check.Url != model.Url && subsBL.GetByUrl(model.Url) != null)
                    {
                        ViewData["ErrorMessage"] = String.Format("This MetaHeader with Url - {0} exists!", model.Url);
                    }
                    else
                    {
                        var subs = new MetaHeader();
                        subs.Id = model.Id;
                        subs.Title = model.Title;
                        subs.Url = model.Url;
                        subs.KeyWord = model.KeyWord;
                        subs.Description = model.Description;

                        int result = subsBL.UpdateMetaHeader(subs);
                        if (result > 0)
                        {
                            return RedirectToAction("Index");
                        }
                        else
                        {
                            ViewData["ErrorMessage"] = "Has an error when executing insert MetaHeader into database";
                        }
                    }
                }
                else
                {
                    ViewData["ErrorMessage"] = String.Format("This MetaHeader not exists!");

                }

            }

            return View(model);
        }

    }
}
